﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Drawing;
namespace QbaoServer
{
    class Program
    {
        static string Read()
        {
            StreamReader sr = new StreamReader("d:\\地址.txt", Encoding.Default);
            String line;
            if ((line = sr.ReadLine()) != null)
            {
                sr.Close();
                return line;
            }
            else
            {
                sr.Close();
                System.Threading.Thread.Sleep(300);
                return Read();
            }

        }
        static void Write(string code)
        {
            try
            {
                FileStream fs = new FileStream("d:\\识别结果.txt", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.Write(code);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            { 
                Console.WriteLine(ex.ToString());
                System.Threading.Thread.Sleep(10000);
            }

            
        }

        static void Main(string[] args)
        {
            Console.WriteLine("验证码识别服务开启. 请不要关闭此窗口");
            string path=null;
            string id = "0";
            while (true)
            {
                path=null;
                try
                {
                    string address = Read();
                    if(address.Split(' ').Length !=2)
                    {
                        continue;
                    }
                    else if( address.Split(' ')[1].IndexOf("jpg") ==-1)
                    {
                        continue;
                    }
                    if (id.IndexOf(address.Split(' ')[0]) != -1)//没有新任务
                    {
                        System.Threading.Thread.Sleep(200);
                        continue;
                    }
                    id = address.Split(' ')[0];
                    path = address.Split(' ')[1];
                }
                catch
                {
                    continue;
                }
                try
                {
                    KUAI.CodeMachine ma = new KUAI.CodeMachine();
                    Image pic = Bitmap.FromFile(path);
                    string code = ma.DIS(path);
                    Write(id + " " + code);
                    System.Threading.Thread.Sleep(200);
                }
                catch
                {
                    break;
                }
                
            }

        }

    }

    
}
